<?php

namespace Biz\model\wiki;


class WikiList extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_list';

    const STATUS_NORMAL = 1;//已关联
    const STATUS_CLOSE = 0;//取消关联
    const STATUS_DEL = 9;//已删除


    public static function getUserWikiList($uid, $limit = [], $order = [])
    {
        $where = [
            'uid' => $uid,
            'status' => self::STATUS_NORMAL
        ];
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        if ($order) {
            $where['ORDER'] = $order;
        }
        return self::selectByWhere($where);
    }

    public static function getUserWikiCount($uid)
    {
        return self::count(['uid' => $uid, 'status' => self::STATUS_NORMAL]);
    }


    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime']) || !$data['addtime']) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['addtime']) || !$data['addtime']) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function getZhichiWikiList($uid, $where="", $limit = [], $order = [])
    {
        $sql = "SELECT wiki_list.* FROM `wiki_list` left join wiki on wiki_list.wiki_id=wiki.id where uid={$uid} {$where} and wiki_list.status!=9 ORDER BY `wiki_list`.`addtime` DESC,`wiki_list`.`id` DESC LIMIT {$limit[1]} OFFSET {$limit[0]}";
        return self::query($sql)->fetchAll();
    }
}
